package org.jiamandan.com.strategy.impl.tram.listener;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jiamandan.com.constant.BizConstant;
import org.jiamandan.com.constant.JSYConstant;
import org.jiamandan.com.dto.JsyBasicDto;
import org.jiamandan.com.strategy.impl.tram.IJSYDZBasicListener;
import org.jiamandan.com.utils.JsyObjectUtil;
import org.springframework.stereotype.Service;


@Service(value = JSYConstant.Service.CMD_JSY_T00205_SERVICE)
@AllArgsConstructor
@Slf4j
public class JSYChargeRecordListener implements IJSYDZBasicListener {


    @Override
    public void doExecute(String payload, ChannelHandlerContext client) throws Exception {
        log.info("嘉满电-JSY结算数据(CMD = T00205)：" + payload);
        JsyBasicDto jsyBasicDto = JsyObjectUtil.parse(payload);
        String[] items = StringUtils.split(jsyBasicDto.getData(), BizConstant.separator);
        log.info("嘉满电-JSY结算数据上报:" + jsyBasicDto.getData());
        log.info("嘉满电-JSY结算数据上报:设备号:" + jsyBasicDto.getDeviceNo() + "订单号" + items[0] + "端口号" + items[1] + "电压:" + items[2] + "电流" + items[3] + "当前功率:" + items[4] + "最大功率:" + items[5] +
                "总时间:" + items[6] + "电能量:" + items[7] + "功率因素:" + items[8] + "继电器状态:" + items[9] +
                "切断原因:" + items[10] + "开始充电时间:" + items[11] + "结束充电时间:" + items[12] + "启动类型:" + items[13] + "设备温度:" + items[14]);
        Long tradeNo = Long.parseLong(items[0], 16);
        log.info("嘉满电-JSY订单号:" + tradeNo);
        reply(client);
    }

    public static void reply(ChannelHandlerContext client) {
        StringBuilder builder = new StringBuilder();
        builder.append("_" + JSYConstant.Cmd.S00205).append("/0002:01").append("\r\n");
        log.info("嘉满电-JSY结算数据应答(CMD = S00205):" + builder.toString());
        ByteBuf buf = Unpooled.buffer(builder.toString().getBytes().length);
        buf.writeBytes(builder.toString().getBytes());
        ChannelFuture future = client.writeAndFlush(buf);
    }

    public static String secondConvertHourMinSecond(Integer second) {
        String str = "00:00:00";
        if (second == null || second < 0) {
            return str;
        }

        // 得到小时
        long h = second / 3600;
        System.out.println(h);
        str = h > 0 ? ((h < 10 ? ("0" + h) : h) + ":") : "00:";

        // 得到分钟
        long m = (second % 3600) / 60;
        str += (m < 10 ? ("0" + m) : m) + ":";

        //得到剩余秒
        long s = second % 60;
        str += (s < 10 ? ("0" + s) : s);
        return str;
    }

    static String getStopReason(String profile) {
        switch (profile) {
            case ("01"):
                return "81";  //指令下发
            case ("02"):
                return "80";  //负载过大
            case ("03"):
                return "75";  //充满自停
            case ("04"):
                return "79";  //用户拔出
            case ("05"):
                return "76";  //到达指定时间
            case ("06"):
                return "25"; //温度过高
            default:
                return profile;
        }
    }
}
